package com.zhoug.widgets.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.zhoug.widgets.R;


/**
 * 文本输入窗口,显示dialog的同时弹出软键盘,
 */
public class EditDialogFragment extends DialogFragment {
    private static final String TAG = "EditDialogFragment";
    private TextView tvDescription;
    private TextView tvBtn;
    private EditText etValue;

    private String description;
    private String value;

    private int width = WindowManager.LayoutParams.MATCH_PARENT;
    private int height = WindowManager.LayoutParams.WRAP_CONTENT;
    private int gravity = Gravity.BOTTOM;

    private View.OnClickListener onConfirmListener;
    private @StyleRes
    int windowAnimationResId = -1;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);

    }

    //也可以用样式设置无标题,动画,弹出软键盘等
   /* @Override
    public int getTheme() {
//        return super.getTheme();
        return R.style.edit_dialog_fragment;
    }*/

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_edit_dialog, container, false);
        //必须设置无标题 否则不会匹配内容
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置左右无缝隙
        Window window1 = getDialog().getWindow();
        if(window1!=null){
            window1.setBackgroundDrawableResource(R.drawable.bg_white);
            window1.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }

        tvDescription = root.findViewById(R.id.tv_description);
        tvBtn = root.findViewById(R.id.tv_btn);
        etValue = root.findViewById(R.id.edit_value);
        if (description != null) {
            tvDescription.setText(description);
        }
        if (value != null) {
            etValue.setText(value);
        }


        if (onConfirmListener != null) {
            tvBtn.setOnClickListener(onConfirmListener);
        }
        //设置动画
        if (windowAnimationResId != -1) {
            Window window = getDialog().getWindow();
            if (window != null) {
                window.setWindowAnimations(windowAnimationResId);
            }
        }
        //无效
        setAttributes(width,height,gravity);
        return root;
    }


    /**
     * 设置动画
     *
     * @param resId R.style.anim_window_popu动 画
     */
    public void setWindowAnimations(@StyleRes int resId) {
        windowAnimationResId = resId;
    }

    public void setDefWindowAnimations() {
        windowAnimationResId =R.style.anim_window_popu;
    }


    public void setDescription(String description) {
        this.description = description;
    }

    public String getValue() {
        if (etValue != null) {
            return etValue.getText().toString();
        }
        return null;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setOnConfirmListener(View.OnClickListener onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
    }

    /**
     * 设置窗口的属性
     *
     * @param width   窗口宽 例：WindowManager.LayoutParams.MATCH_PARENT
     * @param height  高 例：200
     * @param gravity 位子 例：Gravity.BOTTOM
     */
    public void setAttributes(int width, int height, int gravity) {
        this.width = width;
        this.height = height;
        this.gravity = gravity;
        Dialog dialog = getDialog();
        if (dialog != null && dialog.getWindow() != null) {
            WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
            attributes.width = this.width;
            attributes.height = this.height;
            attributes.gravity = this.gravity;
            dialog.getWindow().setAttributes(attributes);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        setAttributes(width,height,gravity);
    }


}
